package Operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation {
    public void work(BookList bookList){
        System.out.println("删除图书！");
        Scanner scanner = new Scanner(System.in) ;
        System.out.println("请输入你要删除的图书名:");
        int index = -1;
        String name = scanner.nextLine();
        int currentSize = bookList.getUsedSize();
        for (int i=0;i<currentSize;i++){
            Book book = bookList.getBooks(i);
            if(book.getName().equals(name)){
                index=i;//找到书，得到该书的下标
                System.out.println("删除成功！ 删除书目为：");
                System.out.println(book);
                break;
            }
        }
        if(index==-1){
            System.out.println("没有你要删除的图书！");
            return;
        }
        //可以删除了
        //移动数据进行覆盖，size--
        for (int j=index;j<currentSize-1;j++){
           // bookList[j]=bookList[j+1];
            //前移 覆盖
            Book book = bookList.getBooks(j+1);
            bookList.setBooks(book,j);
            //图书的size--
            bookList.setUsedSize(currentSize-1);
            //回收被删除的空间
            bookList.setBooks(null,currentSize-1);
        }


    }

}
